module.exports = {
    add(colName, data){
        return new Promise((resolve,reject)=>{
            colName.insertMany(data, err=>{
                if(err){
                    reject(err);
                }else{
                    resolve();
                }
            })
        })
    },
    set(colName, where, data, num=1){
        const type = num===1 ? "updateOne" : "updateMany";
        return new Promise((resolve,reject)=>{
            colName[type](where, data, err=>{
                if(err){
                    reject(err);
                }else{
                    resolve();
                }
            })
        })
    },
    remove(colName, where={}, num=1){
        const type = num===1 ? "deleteOne" : "deleteMany";
        return new Promise((resolve,reject)=>{
            colName[type](where,err=>{
                if(err){
                    reject(err);
                }else{
                    resolve();
                }
            })
        })
    },
    get(colName, where={}){
        // where省略：查所有
        // where：{name:"Admin"}：查指定
        // where：{name:"Admin",pass:123123}：且查询
        // where：{$or:[{name:"Admin"},{pass:123123}]}：或查询
        // where：{price:{$lt:2000,$gt:1000}}：区间查询
        // where：{name:/张/}：模糊查询
        // where：{name:/^张/}：指定开头模糊查询
        return new Promise((resolve,reject)=>{
            colName.find(where, {_id:0,__v:0}, (err,data)=>{
                if(err){
                    reject(err);
                }else{
                    resolve(data);
                }
            })
        })
    },
    getPage(colName,where,index,num){
        return new Promise((resolve,reject)=>{
            colName.find(where, {_id:0,__v:0}).limit(num).skip(num*index).exec((err,data)=>{
                if(err){
                    reject(err);
                }else{
                    resolve(data);
                }
            })
        })
    },
    getSort(colName,where,sortKey){
        return new Promise((resolve,reject)=>{
            colName.find(where, {_id:0,__v:0}).sort(sortKey).exec((err,data)=>{
                if(err){
                    reject(err);
                }else{
                    resolve(data);
                }
            })
        })
    },
    getKind(colName, filed){
        return new Promise((resolve,reject)=>{
            colName.distinct(filed,(err,data)=>{
                if(err){
                    reject(err);
                }else{
                    resolve(data);
                }
            })
        })
    },
    getCount(colName){
        return new Promise((resolve,reject)=>{
            colName.countDocuments((err,data)=>{
                if(err){
                    reject(err);
                }else{
                    resolve(data);
                }
            })
        })
    }
}